package com.winexpress.session.support;

import com.winexpress.entity.Admin;
import com.winexpress.entity.Customer;
import com.winexpress.entity.Enquiry;
import com.winexpress.entity.User;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class EnquirySessionBean {

    @PersistenceContext
    private EntityManager em;

    public Long createEnquiry(Long cid, String subject, String category, String content) {

        Enquiry enquiry = new Enquiry();

        Customer customer = em.find(Customer.class, cid);

        enquiry.setCustomer(customer);
        enquiry.setSubject(subject);
        enquiry.setCategory(category);
        enquiry.setContent(content);
        enquiry.setStatus("pending");
        Date date = new Date();
        enquiry.setDateCreated(date);
        em.persist(enquiry);

        return enquiry.getId();
    }

    public List<String> adminEmailList() {

        Query q = em.createQuery("SELECT s.email FROM User s  WHERE s.adminUser = true");
        List<String> adminemail = q.getResultList();
       
        return adminemail;

    }

    public Enquiry retrieveEnquiry(Long id) {
        Enquiry enq = em.find(Enquiry.class, id);
        return enq;
    }

    public List<Enquiry> listPendingEnquiry() {
        Query q = em.createQuery("SELECT a FROM Enquiry a WHERE a.status = :inID");
        q.setParameter("inID", "pending");
        List<Enquiry> pendingEnquiry = q.getResultList();
        return pendingEnquiry;
    }

    public List<Enquiry> listCustomerEnquiry(String uid) {
        Long userid = Long.parseLong(uid);
        Query q = em.createQuery("SELECT a FROM Enquiry a WHERE a.customer.id = :inID and a.status=:inID2");
        q.setParameter("inID", userid);
        q.setParameter("inID2", "responded");
        List<Enquiry> customerEnquiry = q.getResultList();
        return customerEnquiry;
    }

    public List<Enquiry> listCustomerEnquiryPending(String uid) {
        Long userid = Long.parseLong(uid);
        Query q = em.createQuery("SELECT a FROM Enquiry a WHERE a.customer.id = :inID and a.status=:inID2");
        q.setParameter("inID", userid);
        q.setParameter("inID2", "pending");
        List<Enquiry> customerEnquiry = q.getResultList();
        return customerEnquiry;
    }

    public List<Enquiry> listRespondedEnquiry() {
        Query q = em.createQuery("SELECT a FROM Enquiry a WHERE a.status = :inID");
        q.setParameter("inID", "responded");
        List<Enquiry> respondedEnquiry = q.getResultList();
        return respondedEnquiry;
    }

    public List<Enquiry> listAllEnquiry() {
        Query q = em.createQuery("SELECT a FROM Enquiry a ");
        List<Enquiry> allEnquiry = q.getResultList();
        return allEnquiry;
    }

    public Enquiry setReply(Long eid, Long aid, String reply) {
        Enquiry enq = em.find(Enquiry.class, eid);
        Admin admin = em.find(Admin.class, aid);
        enq.setAdmin(admin);
        enq.setReply(reply);
        Date date = new Date();
        enq.setStatus("responded");
        enq.setDateResponded(date);
        em.persist(enq);
        return enq;
    }

}
